Run Codex from Python
Use run() and run_prompt() for deterministic non-interactive tasks.
A static, double-click documentation set for the local Python SDK wrapper around codex exec.
The SDK now supports sync + async execution, live streaming, retries/backoff, and persistent named sessions.
Use run() and run_prompt() for deterministic non-interactive tasks.
Use run_async(), resume_async(), and async live runs for asyncio workflows.
Use run_live()/run_live_async() to consume JSONL events as they arrive.
Use RetryPolicy on CodexLocalClient for built-in backoff retries.
Use event_hook to capture structured command/retry/live/session events.
Use JsonFileSessionStore with session names to resume across process restarts.
Use run_with_schema() to constrain final output with JSON Schema.
subprocess.Popen and asyncio subprocess APIs.CodexClientEvent + event_hook.from codex_local_sdk import CodexExecRequest, CodexLocalClient, RetryPolicy, SandboxMode
client = CodexLocalClient(
retry_policy=RetryPolicy(max_attempts=3, initial_backoff_seconds=0.5, jitter_ratio=0.2),
)
result = client.run(
CodexExecRequest(
prompt="Summarize this repository in 5 bullets.",
sandbox=SandboxMode.READ_ONLY,
),
timeout_seconds=120,
)
print(result.final_message)
See full setup on the Getting Started page.
| Page | Purpose | Best For |
|---|---|---|
| Getting Started | Install, async setup, retries, and persistent sessions. | Initial SDK onboarding. |
| API Reference | Full class/method reference including async and stores. | Implementation and integration work. |
| Live Runs & Threads | Streaming, session continuation, and async live patterns. | Automation and orchestration. |
| Examples Cookbook | Scenario-based snippets and runnable example mapping. | Copy/paste starting points. |
| Testing & Quality | Suite layout and quality checks for new features. | Contributors and maintainers. |